﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.VehicleTransportFaqCategory;

namespace BCData.TMS.VehicleTransportFaqCategory
{
    public class VehicleTransportFaqCategoryData : IVehicleTransportFaqCategoryData
    {
        private readonly IDatabaseContext databaseContext;
        private readonly string QUERY = @"SELECT
		    VehicleTransportFaqCategoryId,
		    FaqCategory,
		    CreateAdminId,
		    CreateAdminName,
		    CreateTime,
		    UpdateTime,
		    EditAdminId,
		    EditAdminName
        FROM
            vehicletransportfaqcategory";



        #region 构造函数
        public VehicleTransportFaqCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新车辆运输常见问题分类纪录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryEntity"></param>
        /// <returns></returns>
        public async Task<VehicleTransportFaqCategoryEntity> InsertAsync(VehicleTransportFaqCategoryEntity entity)
        {
            return await this.databaseContext.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改车辆运输常见问题分类记录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryEntity"></param>
        /// <returns></returns>
        public async Task<VehicleTransportFaqCategoryEntity> UpdateAsync(VehicleTransportFaqCategoryEntity entity)
        {
            return await this.databaseContext.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除车辆运输常见问题分类记录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long vehicleTransportFaqCategoryId)
        {
            string sql = "DELETE FROM vehicletransportfaqcategory WHERE VehicleTransportFaqCategoryId = ?vehicleTransportFaqCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?VehicleTransportFaqCategoryId", vehicleTransportFaqCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取车辆运输常见问题分类记录
        /// </summary>
        /// <param name="vehicleTransportFaqCategoryId"></param>
        /// <returns></returns>
        public async Task<VehicleTransportFaqCategoryEntity> GetEntityAsync(long vehicleTransportFaqCategoryId)
        {
            string sql = QUERY + " WHERE  VehicleTransportFaqCategoryId = ?vehicleTransportFaqCategoryId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?VehicleTransportFaqCategoryId", vehicleTransportFaqCategoryId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleTransportFaqCategoryEntity>(sql, parameters);
        }
        #endregion

        #region GetByFaqCategory
        /// <summary>
        /// 获取车辆运输常见问题分类记录
        /// </summary>
        /// <param name="faqCategory"></param>
        /// <returns></returns>
        public async Task<VehicleTransportFaqCategoryEntity> GetByFaqCategoryAsync(string faqCategory)
        {
            string sql = QUERY + " WHERE  FaqCategory = ?FaqCategory";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?FaqCategory", faqCategory, DbType.AnsiString);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleTransportFaqCategoryEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询车辆运输常见问题分类记录
        public async Task<(IEnumerable<VehicleTransportFaqCategoryEntity>, int)> GetListAsync(string faqCategory, int pageIndex, int pageSize)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (!string.IsNullOrWhiteSpace(faqCategory))
            {
                wheres.Add(" FaqCategory LIKE ?FaqCategory ");
                parameters.Add("?FaqCategory", $"%{faqCategory}%", DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  vehicletransportfaqcategory  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.databaseContext.QueryMultipleAsync<VehicleTransportFaqCategoryEntity, int>(sql, parameters);
        }
        #endregion


        #region GetListAsync 查询车辆运输常见问题分类记录
        public async Task<IEnumerable<VehicleTransportFaqCategoryEntity>> GetListAsync(string faqCategory)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (!string.IsNullOrWhiteSpace(faqCategory))
            {
                wheres.Add(" FaqCategory LIKE ?FaqCategory ");
                parameters.Add("?FaqCategory", $"%{faqCategory}%", DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  vehicletransportfaqcategory  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : ""
                  );

            return await this.databaseContext.Slave.QueryAsync<VehicleTransportFaqCategoryEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



